using Castle.DynamicProxy;
using Publicuse.Entity;
using Publicuse.Entity.AttributeUtil;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace Publicuse.Autofac
{
    /// <summary>
    /// 异常拦截
    /// </summary>
    public sealed class ExceptionInerceptor : IInterceptor
    {
        private object[] input;

        public void Intercept(IInvocation invocation)
        {
            try
            {
                this.input = invocation.Arguments;
                invocation.Proceed();
            }
            catch (Exception ex)
            {
                var businessType = (BusinessType)input[0];
                var busTypeName = BusinessAttributeUtil.GetAttributeName<BusinessAttribute>(businessType);
                var businessActionType = (BusinessActionType)input[1];
                var busActionName = BusinessAttributeUtil.GetAttributeName<BusinessAttribute>(businessActionType);
                var methodName = input[3].ToString();
                var parameter = input[2];
                var writeLogType = (BusinessLogType)input[4];
                var loginUserCode = HttpContextExtendBase.GetLoginCode();
                Log log = new Log()
                {
                    lmodule = busTypeName + "异常",
                    ltype = busActionName,
                    luser = loginUserCode,
                    lrole = loginUserCode,
                    lsuccess = 2,
                };
                var headers = HttpContextExtendBase.Current.Request.Headers;
                if (headers.ContainsKey("X-Forwarded-For"))
                {
                    HttpContextExtendBase.Current.Connection.RemoteIpAddress = IPAddress.Parse(headers["X-Forwarded-For"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)[0]);
                }
                var remoteIp = LogClass.GetIPAddress() + "|" + headers["X-Real-IP"].ToString() + "|" + headers["X-Forwarded-For"].ToString();
                if (string.Compare(remoteIp, "::1", true) == 0)
                {
                    remoteIp = HttpContextExtendBase.Current.Connection.LocalIpAddress.MapToIPv4()?.ToString();
                }
                if (remoteIp.Split(':').Length >= 3)
                {
                    remoteIp = remoteIp.Split(':')[3];
                }
                var RemoteIpAddress = HttpContextExtendBase.Current.Connection.RemoteIpAddress;
                var sysinfo = $"IP地址：{remoteIp + "|" + RemoteIpAddress}";
                if (parameter != null)
                {
                    log.lcontent = sysinfo + loginUserCode + busActionName + "异常：" + ex.Message + methodName + " 参数信息：" + parameter;
                }
                else
                {
                    log.lcontent = sysinfo + loginUserCode + busActionName + "异常：" + ex.Message + methodName + " 参数信息：" + methodName;
                }

                if (writeLogType == BusinessLogType.writeLogToDataBase || writeLogType == BusinessLogType.writeLogToFileAndDataBase)
                {
                    AutofacLogUtil.GetInstance.WriteDataBase(log);
                }

                if (writeLogType == BusinessLogType.writeLogToFile || writeLogType == BusinessLogType.writeLogToFileAndDataBase)
                    AutofacLogUtil.GetInstance.WriteLogOld(log.lmodule, log.lcontent, true);
            }
        }
    }
}
